forum

home / developersection / forums / search for a header and put that header in a list

Search for a header and put that header in a list

Royce Roy 2032 29-Mar-2014

I have a textfile with blogposts, each blogpost is divided in a header and content shown below

    #Header

    A post        

    #Content

    My content goes here...

    #Header

    Another post

    #Content

    My content goes here...

Now I want to grab all the headers, the text that comes after #Header and before #Content and put that in a List<>. How can I do that?

This is what I have come up with so far:

var headers = new List<string>();

using (StreamReader reader = new StreamReader(Path))

        {

            string line;

            while ((line = reader.ReadLine()) != null)

            {

                headers.AddRange(line)

                    .SkipWhile(l => l != "#Header")

                    .Skip(1)

                    .TakeWhile(l => !l.StartsWith("#"))

                    .ToList();

            }

        }

Linq is preferred.


c# c# 
Updated on 29-Mar-2014
Royce Roy

Other


Message
Can you answer this question?

Answer

1 Answers

Liked By